package com.yueka.controller;

import com.yueka.pojo.R;
import com.yueka.service.AliOssService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.dromara.x.file.storage.core.presigned.GeneratePresignedUrlResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/file")
public class UploadController {
    @Resource
    AliOssService aliOssService;

    /**
     * 上传文件并返回访问地址
     */
    @PostMapping("/upload")
    public R uploadFile(@RequestPart("file") MultipartFile file) throws Exception {
        return R.success(aliOssService.uploadFile(file));
    }

    /**
     * 客户端上传
     */
    @PostMapping("/clientUpload")
    public R<GeneratePresignedUrlResult> clientUpload() {
        return R.success(aliOssService.clientUpload());
    }

    /**
     * 预签名
     */
    @GetMapping("/preSign/**")
    public void preSign(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取完整的请求 URI
        String uri = request.getRequestURI();
        response.sendRedirect(aliOssService.preSign(uri));
    }

    /**
     * 获取签名后地址
     *
     * @param url
     * @return
     */
    @GetMapping("/getSignUrl")
    public R<String> getSignUrl(@RequestParam("url") String url) {
        return R.success(aliOssService.getSignUrl(url));
    }
}